package com.qf.java2106.huangshengrui.hotel.controller.backend;

import com.alibaba.fastjson.JSON;
import com.qf.java2106.huangshengrui.hotel.common.ResultVO;
import com.qf.java2106.huangshengrui.hotel.constant.MethodConstant;
import com.qf.java2106.huangshengrui.hotel.controller.BaseServlet;
import com.qf.java2106.huangshengrui.hotel.factory.BeanFactory;
import com.qf.java2106.huangshengrui.hotel.pojos.FoodType;
import com.qf.java2106.huangshengrui.hotel.service.IFoodTypeService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author q
 * @version V1.0
 * @date 2021/10/25
 **/
@WebServlet("/foodtype")
public class FoodTypeController extends BaseServlet {

    private IFoodTypeService foodTypeService = (IFoodTypeService) BeanFactory.getBean("foodTypeService");

    /**
     * 判断用户权限
     * @param request
     * @param response
     * @return true为管理员
     * @throws IOException
     * @throws ServletException
     */
    public boolean checkUser(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Integer userIsAdmin = (Integer) request.getSession().getAttribute("userIsAdmin");
        if (userIsAdmin == 1){
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据key查找菜系
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public String search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        boolean b = checkUser(request, response);
        if (!b){
            return "<script>alert('您不是管理员，无权操作~');alert('请点击右侧退出重新登陆~');</script>";
        }
        String keyword = request.getParameter("keyword");
        ResultVO vo = foodTypeService.findTypeLikeKey(keyword);
        if (vo.getSuccess()){
            request.setAttribute("types",vo.getData());
            request.setAttribute("keyword",keyword);
            return MethodConstant.FORWARD_PREFIX + request.getContextPath()+"/backend/detail/foodtype/foodtype-list.jsp";
        } else {
            return (String) vo.getData();
        }
    }

    /**
     * 新增菜系
     * @param request
     * @param response
     */
    public String save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        FoodType foodType = new FoodType();
        foodType.setTypeName(name.trim());
        ResultVO vo = foodTypeService.saveType(foodType);
        if (vo.getSuccess()){
            return MethodConstant.REDIRECT_PREFIX + request.getContextPath()+"/foodtype?method=search";
        } else {
            return (String) vo.getData();
        }
    }

    /**
     * 修改ui
     * @param request
     * @param response
     */
    public String updateUi(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Integer id = Integer.valueOf(request.getParameter("typeid"));
        ResultVO vo = foodTypeService.findTypeById(id);
        if (vo.getSuccess()){
            request.setAttribute("foodType",vo.getData());
            String backendDetailFolder = (String) getServletContext().getAttribute("backendDetailFolder");
            return MethodConstant.FORWARD_PREFIX + "/backend/detail/foodtype/foodtype-update.jsp";
        } else {
            return (String) vo.getData();
        }
    }

    /**
     * 修改提交
     * @param request
     * @param response
     */
    public String update(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        try {
            //如何快速封装前台请求参数到一个Java实体数据
            Map<String, String[]> parameterMap = request.getParameterMap();
            //实体模型
            FoodType foodType = new FoodType();
            //把map中的数据封装到一个实体模型中。有一个要求，map的key必须和Java实体属性名相同
            BeanUtils.populate(foodType, parameterMap);
            ResultVO vo = foodTypeService.updateType(foodType);
            if (vo.getSuccess()){
                return MethodConstant.REDIRECT_PREFIX + request.getContextPath() + "/foodtype?method=search";
            } else {
                return (String) vo.getData();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }

    }

    /**
     * 删除菜系
     * @param request
     * @param response
     */
    public String delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer id = Integer.valueOf(request.getParameter("typeid"));
        ResultVO checkVo = foodTypeService.checkDeleteType(id);
        if (!checkVo.getSuccess()){
            return "<script>location.href='"+request.getContextPath()+"/foodtype?method=search';alert('"+checkVo.getMessage()+"');</script>";
        }
        ResultVO vo = foodTypeService.deleteType(id);
        if (vo.getSuccess()){
            return MethodConstant.REDIRECT_PREFIX + request.getContextPath()+"/foodtype?method=search";
        } else {
            return (String) vo.getData();
        }
    }

    /**
     * 检查用户名是否存在
     * @param request
     * @param response
     */
    public String nameExists(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeName = request.getParameter("typeName");
        ResultVO vo = foodTypeService.findTypeByName(typeName);
        return JSON.toJSONString(vo);
    }

}
